<template>
  <div>
    <el-dialog
      :title="info.isAdd ? '分类添加' : '分类修改'"
      :visible.sync="info.isshow"
      @close="close"
    >
      user:{{ user }}
      <el-form :model="user">
        <el-form-item label="上级分类" label-width="120px">
          <el-select v-model="user.pid">
            <el-option label="顶级分类" :value="0"></el-option>
            <!-- 遍历arr  -->
            <el-option
              v-for="item in showArr"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="分类名称" label-width="120px">
          <el-input v-model="user.catename" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="图片" label-width="120px">
          <upload-img @sendFile="user.img = $event" ref="upload"></upload-img>
        </el-form-item>

        <el-form-item label="状态" label-width="120px">
          <el-switch
            v-model="user.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" v-if="info.isAdd" @click="add"
          >添 加</el-button
        >
        <el-button type="primary" v-else @click="update">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import { reqcateadd, reqcateedit, reqcateinfo } from "../../../http/api";
import { formClose } from "../../../mixins";
import { successAlert } from "../../../utils/alert";

export default {
  // 4.接收info
  props: ["info"],
  mixins: [formClose],
  data() {
    return {
      // 5.1 初始化表单数据
      user: {
        pid: "",
        catename: "",
        img: null,
        status: 1,
      },
    };
  },
  computed: {
    ...mapGetters({
      arr: "cate/arr",
    }),
    showArr() {
      return this.arr.filter((item) => item.id !== this.user.id);
    },
  },
  mounted() {},
  methods: {
    ...mapActions({
      reqArr: "cate/reqArr",
    }),

    // 清空user
    empty() {
      this.user = {
        pid: "",
        catename: "",
        img: null,
        status: 1,
      };
      this.$refs.upload.setImgUrl("");
    },
    // 5.4 点了添加
    add() {
      reqcateadd(this.user).then((res) => {
        if (res.data.code == 200) {
          // ①弹成功
          successAlert(res.data.msg);
          // ②弹框内容清空
          this.empty();
          // ③弹框消失
          this.info.isshow = false;
          // ④重新请求列表
          this.reqArr();
        }
      });
    },

    // 8. 获取一条数据
    getOne(id) {
      reqcateinfo({ id: id }).then((res) => {
        if (res.data.code === 200) {
          this.user = res.data.list;
          this.user.id = id;
          this.$refs.upload.setImgUrl(this.$pre + this.user.img);
        }
      });
    },
    // 9.修改
    update() {
      reqcateedit(this.user).then((res) => {
        if (res.data.code == 200) {
          // ①弹成功
          successAlert(res.data.msg);

          // ②弹框内容清空
          this.empty();
          // ③弹框消失
          this.info.isshow = false;
          // ④重新请求列表
          this.reqArr();
        }
      });
    },
  },
};
</script>
